#include "mytreewidget.h"

MyTreeWidget::MyTreeWidget(QWidget *parent):QTreeWidget(parent)
{
    //间隔颜色
    setAlternatingRowColors(true);

    m_action_add_item = new QAction("add child", this);
    m_action_del_item = new QAction("delete", this);
    m_action_refresh_res_item = new QAction("refresh", this);

    m_current_click_item = NULL;
    connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)), this, SLOT(itemPressedSlot(QTreeWidgetItem*, int)));
    connect(m_action_add_item, SIGNAL(triggered(bool)), this, SLOT(on_addItem()));
    connect(m_action_del_item, SIGNAL(triggered(bool)), this, SLOT(on_delItem()));
    connect(m_action_refresh_res_item, SIGNAL(triggered(bool)), this, SLOT(on_refresh()));

    m_img_list << "";
}

MyTreeWidget:: ~MyTreeWidget()
{

}

void MyTreeWidget::on_refresh()
{
    if (m_current_click_item == NULL)
        return;

    QString item_text = m_current_click_item->text(0);

    if (item_text == "resources")
        refreshResourcesTw();
}

void MyTreeWidget::on_addItem()
{
    if (m_current_click_item == NULL)
        return;

    QString item_text = m_current_click_item->text(0);
    if (item_text == "video_streams")
        addVideoStremItemChildren(m_current_click_item, m_format->video_streams.at(0));
    if (item_text == "lives")
        addLiveItemChildren(m_current_click_item, m_format->video_streams.at(0).lives.at(0));
    if (item_text == "screens")
    {
        emit add_screen();
    }
    if (item_text == "partitions")
    {
        emit add_partition(getIdFromItemText(m_current_click_item->parent()->text(0)));
    }
    if (item_text == "layouts")
    {
        emit add_layout(getIdFromItemText(m_current_click_item->parent()->parent()->parent()->text(0)),
                        getIdFromItemText(m_current_click_item->parent()->text(0)));
    }
    if (item_text == "items")
    {
        emit add_item(getIdFromItemText(m_current_click_item->parent()->parent()->parent()->parent()->parent()->text(0)),
                      getIdFromItemText(m_current_click_item->parent()->parent()->parent()->text(0)),
                      getIdFromItemText(m_current_click_item->parent()->text(0)));
    }
    if (item_text.contains("item") && item_text != "items")
    {
        emit add_cabpos(getIdFromItemText(m_current_click_item->parent()->parent()->parent()->parent()->parent()->parent()->text(0)),
                        getIdFromItemText(m_current_click_item->parent()->parent()->parent()->parent()->text(0)),
                        getIdFromItemText(m_current_click_item->parent()->parent()->text(0)),
                        getIdFromItemText(m_current_click_item->text(0)));
    }
}

void MyTreeWidget::on_delItem()
{
    if (m_current_click_item == NULL)
        return;

    QString item_text = m_current_click_item->text(0);
    if (item_text == "video_stream" || (item_text.contains("live") && item_text != "lives"))
    {
        removeCurrentClickItem();
    }
    else if (item_text.contains("screen") && item_text != "screens")
    {
        int screen_id = getIdFromItemText(item_text);
        emit screen_item_clicked(getIdFromItemText(item_text));
        emit del_screen(screen_id);
        m_screens_item_map.remove(screen_id);
        removeCurrentClickItem();
    }
    else if (item_text.contains("partition") && item_text != "partitions")
    {
        int screen_id = getIdFromItemText(m_current_click_item->parent()->parent()->text(0));
        int partition_id = getIdFromItemText(item_text);
        emit del_partition(screen_id, partition_id);
        m_screens_item_map[screen_id].partitions_item_map.remove(partition_id);
        removeCurrentClickItem();
    }
    else if (item_text.contains("layout") && item_text != "layouts")
    {
        int screen_id = getIdFromItemText(m_current_click_item->parent()->parent()->parent()->parent()->text(0));
        int partition_id = getIdFromItemText(m_current_click_item->parent()->parent()->text(0));
        int layout_id = getIdFromItemText(item_text);
        emit del_layout(screen_id, partition_id, layout_id);
        m_screens_item_map[screen_id].partitions_item_map[partition_id].layouts_item_map.remove(layout_id);
        removeCurrentClickItem();
    }
    else if (item_text.contains("item") && item_text != "items")
    {
        int screen_id = getIdFromItemText(m_current_click_item->parent()->parent()->parent()->parent()->parent()->parent()->text(0));
        int partition_id = getIdFromItemText(m_current_click_item->parent()->parent()->parent()->parent()->text(0));
        int layout_id = getIdFromItemText(m_current_click_item->parent()->parent()->text(0));
        int item_id = getIdFromItemText(item_text);
        emit del_item(screen_id, partition_id, layout_id, item_id);
        m_screens_item_map[screen_id].partitions_item_map[partition_id].layouts_item_map[layout_id].items_item_map.remove(item_id);
        removeCurrentClickItem();
    }
    else if (item_text.contains("cab") && item_text.contains("position"))
    {
        int screen_id = getIdFromItemText(m_current_click_item->parent()->parent()->parent()->parent()->parent()->parent()->parent()->text(0));
        int partition_id = getIdFromItemText(m_current_click_item->parent()->parent()->parent()->parent()->parent()->text(0));
        int layout_id = getIdFromItemText(m_current_click_item->parent()->parent()->parent()->text(0));
        int item_id = getIdFromItemText(m_current_click_item->parent()->text(0));

        emit del_cabpos(screen_id, partition_id, layout_id, item_id, item_text);

        m_screens_item_map[screen_id].partitions_item_map[partition_id].layouts_item_map[layout_id].items_item_map[item_id].cab_pos_items_item_map.remove(item_text);

        removeCurrentClickItem();
    }
}

void MyTreeWidget::removeCurrentClickItem()
{
    m_current_click_item->parent()->removeChild(m_current_click_item);
    delete m_current_click_item;
    m_current_click_item = NULL;

    //删除节点之后更新一下format数据
    getAllDataFromTwView();
}
void MyTreeWidget::removeScreenItem(int screen_id)
{
    m_current_click_item = m_screens_item_map[screen_id].screen_item_title;
    removeCurrentClickItem();
}

int MyTreeWidget::getIdFromItemText(QString item_text)
{
    if (item_text.contains("screen") || item_text.contains("layout"))
        return item_text.right(item_text.size() - 6).toInt();
    else if (item_text.contains("partition"))
        return item_text.right(item_text.size() - 9).toInt();
    else if (item_text.contains("item"))
        return item_text.right(item_text.size() - 4).toInt();
    else {}

    return 1;
}

void MyTreeWidget::itemPressedSlot(QTreeWidgetItem *pressed_item, int column)
{
    if (qApp->mouseButtons() == Qt::RightButton && column == 0)   //判断是否为右键
    {
        m_current_click_item = pressed_item;
        QMenu *menu = new QMenu(this);
        QString item_text = pressed_item->text(0);
        if (item_text == "video_streams" || item_text == "lives")
        {
            menu->addAction(m_action_add_item);
            menu->exec(QCursor::pos());   //菜单弹出位置为鼠标点击位置

        } else if (item_text == "resources")
        {
            menu->addAction(m_action_refresh_res_item);
            menu->exec(QCursor::pos());

        } else if (item_text == "screens" ||
                   item_text == "partitions" ||
                   item_text == "layouts" ||
                   item_text == "items")
        {
            menu->addAction(m_action_add_item);
            menu->exec(QCursor::pos());

        } else if ((item_text == "video_stream") || (item_text.contains("live") && item_text != "lives") ||
                   (item_text.contains("screen") && item_text != "screens") ||
                   (item_text.contains("partition") && item_text != "partitions") ||
                   (item_text.contains("layout") && item_text != "layouts") ||
                   (item_text.contains("cab") && item_text.contains("position")))
        {
            menu->addAction(m_action_del_item);
            menu->exec(QCursor::pos());

        } else if (item_text.contains("item") && item_text != "items")
        {
            menu->addAction(m_action_add_item);
            menu->addAction(m_action_del_item);
            menu->exec(QCursor::pos());
        }

    } else if (qApp->mouseButtons() == Qt::LeftButton && column == 0)
    {
        m_current_click_item = pressed_item;
        QString current_item_text = pressed_item->text(0);
        if (current_item_text.contains("screen") && current_item_text != "screens")
        {
            int screen_id = getIdFromItemText(current_item_text);
            emit screen_item_clicked(screen_id);

        } else if (current_item_text.contains("partition") && current_item_text != "partitions")
        {
            int partition_id = getIdFromItemText(current_item_text);
            QString screen_item_text = m_current_click_item->parent()->parent()->text(0);
            int screen_id = getIdFromItemText(screen_item_text);
            emit partition_item_clicked(screen_id, partition_id);
        } else if (current_item_text.contains("layout") && current_item_text != "layouts")
        {
            int layout_id = getIdFromItemText(current_item_text);
            QString partition_item_text = m_current_click_item->parent()->parent()->text(0);
            int partition_id = getIdFromItemText(partition_item_text);
            QString screen_item_text = m_current_click_item->parent()->parent()->parent()->parent()->text(0);
            int screen_id = getIdFromItemText(screen_item_text);
            emit layout_item_clicked(screen_id, partition_id, layout_id);
        } else if (current_item_text.contains("cab") && current_item_text.contains("position"))
        {
            QString item_item_text = m_current_click_item->parent()->text(0);
            int item_id = getIdFromItemText(item_item_text);
            QString layout_item_text = m_current_click_item->parent()->parent()->parent()->text(0);
            int layout_id = getIdFromItemText(layout_item_text);
            QString partition_item_text = m_current_click_item->parent()->parent()->parent()->parent()->parent()->text(0);
            int partition_id = getIdFromItemText(partition_item_text);
            QString screen_item_text = m_current_click_item->parent()->parent()->parent()->parent()->parent()->parent()->parent()->text(0);
            int screen_id = getIdFromItemText(screen_item_text);
            emit item_item_clicked(screen_id, partition_id, layout_id, item_id, current_item_text);
        }
        emit update_clicked();
    }
}

void MyTreeWidget::getMaterialOrStreamIndexDataFromTw(QTreeWidgetItem *material_or_stream_index_item_root, CabPosItem &one_cab_pos_item)
{
    if (one_cab_pos_item.type != "video")
    {
        for (int i = 0; i < material_or_stream_index_item_root->childCount(); i++)
        {
            Material one_material;
            one_material.duration = getLineEditItemChildText(material_or_stream_index_item_root->child(i), 0).toInt();
            one_material.content = getLineEditItemChildText(material_or_stream_index_item_root->child(i), 1);
            one_cab_pos_item.data.materials[i] = one_material;
        }
    } else
    {
        int all = getLineEditItemText(material_or_stream_index_item_root->child(0)).toInt();
        one_cab_pos_item.data.stream_index.all = all;
    }
}

void MyTreeWidget::getCabPosItemDataFromTw(QTreeWidgetItem *cab_pos_item_root, Item &one_item)
{
    for (int i = 0; i < cab_pos_item_root->childCount(); i++)
    {
        CabPosItem one_cab_pos_item;
        one_cab_pos_item.id     = getLabelItemChildText(cab_pos_item_root->child(i), 0).toInt();
        one_cab_pos_item.type   = getLabelItemChildText(cab_pos_item_root->child(i), 1);
        one_cab_pos_item.x      = getLineEditItemChildText(cab_pos_item_root->child(i), 2).toInt();
        one_cab_pos_item.y      = getLineEditItemChildText(cab_pos_item_root->child(i), 3).toInt();
        one_cab_pos_item.width  = getLineEditItemChildText(cab_pos_item_root->child(i), 4).toInt();
        one_cab_pos_item.height = getLineEditItemChildText(cab_pos_item_root->child(i), 5).toInt();
        if (one_cab_pos_item.type == "text")
        {
            one_cab_pos_item.font   = getLineEditItemChildText(cab_pos_item_root->child(i), 6);
            one_cab_pos_item.size   = getLineEditItemChildText(cab_pos_item_root->child(i), 7).toInt();
            one_cab_pos_item.bold   = getComboBoxItemChildBool(cab_pos_item_root->child(i), 8);
            one_cab_pos_item.italic = getComboBoxItemChildBool(cab_pos_item_root->child(i), 9);
            one_cab_pos_item.fore_color = getLineEditItemChildText(cab_pos_item_root->child(i), 10);
            one_cab_pos_item.back_color  = getLineEditItemChildText(cab_pos_item_root->child(i), 11);
            one_cab_pos_item.background  = getComboBoxItemChildText(cab_pos_item_root->child(i), 12);
            one_cab_pos_item.background_color  = getLineEditItemChildText(cab_pos_item_root->child(i), 13);
            one_cab_pos_item.align       = getComboBoxItemChildIndex(cab_pos_item_root->child(i), 14);
            one_cab_pos_item.effect      = getLineEditItemChildText(cab_pos_item_root->child(i), 15).toInt();
            one_cab_pos_item.speed       = getLineEditItemChildText(cab_pos_item_root->child(i), 16).toInt();
            QTreeWidgetItem *material_item_root = cab_pos_item_root->child(i)->child(17)->child(0);
            getMaterialOrStreamIndexDataFromTw(material_item_root, one_cab_pos_item);
        }

        if (one_cab_pos_item.type == "video")
        {
            one_cab_pos_item.sound = getComboBoxItemChildBool(cab_pos_item_root->child(i), 6);
            QTreeWidgetItem *stream_index_item_root = cab_pos_item_root->child(i)->child(7)->child(0);
            getMaterialOrStreamIndexDataFromTw(stream_index_item_root, one_cab_pos_item);
        }

        if (one_cab_pos_item.type == "image")
        {
            QTreeWidgetItem *material_item_root = cab_pos_item_root->child(i)->child(6)->child(0);
            getMaterialOrStreamIndexDataFromTw(material_item_root, one_cab_pos_item);
        }


        one_item.cabpositems[cab_pos_item_root->child(i)->text(0)] = one_cab_pos_item;
    }
}

void MyTreeWidget::getItemDataFromTw(QTreeWidgetItem *item_item_root, Layout &one_layout)
{
    for (int i = 0; i < item_item_root->childCount(); i++)
    {
        Item one_item;

        QTreeWidgetItem *cab_pos_item_root = item_item_root->child(i);

        one_item.id = getIdFromItemText(cab_pos_item_root->text(0));

        getCabPosItemDataFromTw(cab_pos_item_root, one_item);

        one_layout.items.push_back(one_item);
    }
}

void MyTreeWidget::getLayoutDataFromTw(QTreeWidgetItem *layout_item_root, Partition &one_partition)
{
    for (int i = 0; i < layout_item_root->childCount(); i++)
    {
        Layout one_layout;
        one_layout.id       = getLabelItemChildText(layout_item_root->child(i), 0).toInt();
        one_layout.duration = getLineEditItemChildText(layout_item_root->child(i), 1).toInt();

        QTreeWidgetItem *item_item_root = layout_item_root->child(i)->child(2);
        getItemDataFromTw(item_item_root, one_layout);

        one_partition.layouts[one_layout.id] = one_layout;
    }
}

void MyTreeWidget::getPartitionDataFromTw(QTreeWidgetItem *partition_item_root, Screen &one_screen)
{
    for (int i = 0; i < partition_item_root->childCount(); i++)
    {
        Partition one_partition;
        one_partition.id   = getLabelItemChildText(partition_item_root->child(i), 0).toInt();
        Common common;
        common.x           = getLineEditItemChildText(partition_item_root->child(i)->child(1), 0).toInt();
        common.y           = getLineEditItemChildText(partition_item_root->child(i)->child(1), 1).toInt();
        common.width       = getLineEditItemChildText(partition_item_root->child(i)->child(1), 2).toInt();
        common.height      = getLineEditItemChildText(partition_item_root->child(i)->child(1), 3).toInt();
        common.z_order     = getLineEditItemChildText(partition_item_root->child(i)->child(1), 4).toInt();
        common.transparent = getComboBoxItemChildBool(partition_item_root->child(i)->child(1), 5);
        common.background  = getComboBoxItemChildText(partition_item_root->child(i)->child(1), 6);
        common.background_color  = getLineEditItemChildText(partition_item_root->child(i)->child(1), 7);
        one_partition.common = common;
        QTreeWidgetItem *layout_item_root = partition_item_root->child(i)->child(2);
        getLayoutDataFromTw(layout_item_root, one_partition);

        one_screen.partitions[one_partition.id] = one_partition;
    }
}

void MyTreeWidget::getScreenDataFromTw()
{
    for (int i = 0; i < m_screens_item_root->childCount(); i++)
    {
        Screen one_screen;
        one_screen.id = getLabelItemChildText(m_screens_item_root->child(i), 0).toInt();
        one_screen.name = getLabelItemChildText(m_screens_item_root->child(i), 1);

        QTreeWidgetItem *partition_item_root = m_screens_item_root->child(i)->child(2);
        getPartitionDataFromTw(partition_item_root, one_screen);

        m_format->screens[one_screen.id] = one_screen;
    }
}

//tab->mainwindow->tree更新数据
void MyTreeWidget::updateItemItemData(QTreeWidgetItem *parent_item, Item item)
{

}

void MyTreeWidget::updatePartitionItemData(QTreeWidgetItem *parent_item, Partition partition)
{

}

void MyTreeWidget::getAllDataFromTwView()
{
    m_format->path_refix.file = getLineEditItemChildText(m_path_prefix_item_root, 0);
    m_format->path_refix.url = getLineEditItemChildText(m_path_prefix_item_root, 1);

    m_format->video_streams.clear();
    for (int i = 0; i < m_video_streams_item_root->childCount(); i++)
    {
        VideoStream one_stream;
        one_stream.stream_destination = getLineEditItemChildText(m_video_streams_item_root->child(i), 0).toInt();
        for (int j = 0; j < m_video_streams_item_root->child(i)->child(1)->childCount(); j++)
        {
            Live one_live;
            one_live.interface = getLineEditItemChildText(m_video_streams_item_root->child(i)->child(1)->child(j), 0).toInt();
            one_live.ip = getLineEditItemChildText(m_video_streams_item_root->child(i)->child(1)->child(j), 1);
            one_live.port = getLineEditItemChildText(m_video_streams_item_root->child(i)->child(1)->child(j), 2).toInt();
            one_stream.lives.push_back(one_live);
        }
        one_stream.local = getLineEditItemChildText(m_video_streams_item_root->child(i), 2);
        m_format->video_streams.push_back(one_stream);
    }

    m_format->current_show = getLineEditItemText(m_current_show_item_root);

    m_format->resolution.width = getLineEditItemChildText(m_resolution_item_root, 0).toInt();
    m_format->resolution.height = getLineEditItemChildText(m_resolution_item_root, 1).toInt();

    m_format->resources.clear();
    QFileInfoList list1 = getFileList(g_pro_data.path + QString("/res"));
    QFileInfoList list2 = getFileList(g_pro_data.path);
    QFileInfoList list = list1 + list2;
    int size = list.size();
    for (int i = 0; i < size; ++i)
    {
        QFileInfo fileInfo = list.at(i);
        if (fileInfo.fileName() == "res.zip")
        {
            QFile file(fileInfo.filePath());
            file.open(QIODevice::ReadOnly);
            QByteArray ba = QCryptographicHash::hash(file.readAll(), QCryptographicHash::Md5);
            m_format->resources["res.zip"] = QString(ba.toHex().constData());
            file.close();
        }
    }

    m_format->screens.clear();
    getScreenDataFromTw();
}

void MyTreeWidget::createPathPrefixTw(Format *format)
{
    m_format = format;

    m_path_prefix_item_root = new QTreeWidgetItem;
    m_path_prefix_item_root->setText(0, tr("path_prefix"));
    addTopLevelItem(m_path_prefix_item_root);

    QTreeWidgetItem *file_item = new QTreeWidgetItem;
    file_item->setText(0, tr("file"));
    QTreeWidgetItem *url_item = new QTreeWidgetItem;
    url_item->setText(0, tr("url"));

    m_path_prefix_item_root->addChild(file_item);
    m_path_prefix_item_root->addChild(url_item);

    //此处一定要先addChild才能添加自定义窗口
    QLineEdit *file_edit = new QLineEdit;
    file_edit->setText(format->path_refix.file);
    file_edit->setStyleSheet(m_edit_qss);
    setItemWidget(file_item, 1, file_edit);
    QLineEdit *ftp_edit = new QLineEdit;
    ftp_edit->setStyleSheet(m_edit_qss);
    ftp_edit->setText(format->path_refix.url);
    QCompleter *completer = new QCompleter(content_list);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    ftp_edit->setCompleter(completer);
    setItemWidget(url_item, 1, ftp_edit);
}

void MyTreeWidget::addLiveItemChildren(QTreeWidgetItem *parent_item, Live one_live)
{
    QTreeWidgetItem *interface_item = new QTreeWidgetItem;
    interface_item->setText(0, tr("interface"));
    QTreeWidgetItem *ip_item = new QTreeWidgetItem;
    ip_item->setText(0, tr("ip"));
    QTreeWidgetItem *port_item = new QTreeWidgetItem;
    port_item->setText(0, tr("port"));
    QTreeWidgetItem *live_item_title = new QTreeWidgetItem;
    live_item_title->setText(0, QString("live"));

    parent_item->addChild(live_item_title);
    live_item_title->addChild(interface_item);
    live_item_title->addChild(ip_item);
    live_item_title->addChild(port_item);

    QLineEdit *interface_edit = new QLineEdit;
    interface_edit->setText(QString::number(one_live.interface));
    interface_edit->setStyleSheet(m_edit_qss);
    setItemWidget(interface_item, 1, interface_edit);
    QLineEdit *ip_edit = new QLineEdit;
    ip_edit->setText(one_live.ip);
    ip_edit->setStyleSheet(m_edit_qss);
    setItemWidget(ip_item, 1, ip_edit);
    QLineEdit *port_edit = new QLineEdit;
    port_edit->setText(QString::number(one_live.port));
    port_edit->setStyleSheet(m_edit_qss);
    setItemWidget(port_item, 1, port_edit);
}

void MyTreeWidget::addVideoStremItemChildren(QTreeWidgetItem *parent_item, VideoStream video_stream)
{
    VideoStreamItemChild one_video_stream_item_child;
    one_video_stream_item_child.video_stream_item_title = new QTreeWidgetItem;
    one_video_stream_item_child.video_stream_item_title->setText(0, "video_stream");
    one_video_stream_item_child.stream_destination_item = new QTreeWidgetItem;
    one_video_stream_item_child.stream_destination_item->setText(0, tr("stream_destination"));
    one_video_stream_item_child.live_item_root = new QTreeWidgetItem;
    one_video_stream_item_child.live_item_root->setText(0, tr("lives"));
    one_video_stream_item_child.local_item = new QTreeWidgetItem;
    one_video_stream_item_child.local_item->setText(0, tr("local"));

    parent_item->addChild(one_video_stream_item_child.video_stream_item_title);
    one_video_stream_item_child.video_stream_item_title->addChild(one_video_stream_item_child.stream_destination_item);
    one_video_stream_item_child.video_stream_item_title->addChild(one_video_stream_item_child.live_item_root);
    one_video_stream_item_child.video_stream_item_title->addChild(one_video_stream_item_child.local_item);

    QLineEdit *stream_destination_edit = new QLineEdit;
    stream_destination_edit->setText(QString::number(video_stream.stream_destination));
    stream_destination_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_video_stream_item_child.stream_destination_item, 1, stream_destination_edit);
    QLineEdit *local_edit = new QLineEdit;
    local_edit->setStyleSheet(m_edit_qss);
    local_edit->setText(video_stream.local);
    QCompleter *completer = new QCompleter(content_list);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    local_edit->setCompleter(completer);
    setItemWidget(one_video_stream_item_child.local_item, 1, local_edit);

    for (int i = 0; i < video_stream.lives.size(); i++)
        addLiveItemChildren(one_video_stream_item_child.live_item_root, video_stream.lives.at(i));
}

void MyTreeWidget::createVideoStreamsTw(Format *format)
{
    m_video_streams_item_root = new QTreeWidgetItem;
    m_video_streams_item_root->setText(0, tr("video_streams"));
    addTopLevelItem(m_video_streams_item_root);

    for (int i = 0; i < format->video_streams.size(); i++)
        addVideoStremItemChildren(m_video_streams_item_root, format->video_streams.at(i));
}

void MyTreeWidget::createArchitectureVersionTw(Format *format)
{
    m_architecture_version_item_root = new QTreeWidgetItem;
    m_architecture_version_item_root->setText(0, tr("architecture_version"));
    addTopLevelItem(m_architecture_version_item_root);

    QLabel *architecture_version_label = new QLabel;
    architecture_version_label->setText(format->architecture_version);
    architecture_version_label->setStyleSheet(m_edit_qss);
    setItemWidget(m_architecture_version_item_root, 1, architecture_version_label);
}

void MyTreeWidget::createCurrentShowTw(Format *format)
{
    m_current_show_item_root = new QTreeWidgetItem;
    m_current_show_item_root->setText(0, tr("current_show"));
    addTopLevelItem(m_current_show_item_root);

    QLineEdit *current_show_edit = new QLineEdit;
    current_show_edit->setText(format->current_show);
    current_show_edit->setStyleSheet(m_edit_qss);
    QCompleter *completer = new QCompleter(content_list);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    current_show_edit->setCompleter(completer);
    setItemWidget(m_current_show_item_root, 1, current_show_edit);
}

void MyTreeWidget::createResolutionTw(Format *format)
{
    m_resolution_item_root = new QTreeWidgetItem;
    m_resolution_item_root->setText(0, tr("resolution"));
    addTopLevelItem(m_resolution_item_root);

    QTreeWidgetItem *width_item = new QTreeWidgetItem;
    width_item->setText(0, tr("width"));
    QTreeWidgetItem *height_item = new QTreeWidgetItem;
    height_item->setText(0, tr("height"));
    m_resolution_item_root->addChild(width_item);
    m_resolution_item_root->addChild(height_item);

    QLineEdit *width_edit = new QLineEdit;
    width_edit->setText(QString::number(format->resolution.width));
    width_edit->setStyleSheet(m_edit_qss);
    setItemWidget(width_item, 1, width_edit);
    QLineEdit *height_edit = new QLineEdit;
    height_edit->setText(QString::number(format->resolution.height));
    height_edit->setStyleSheet(m_edit_qss);
    setItemWidget(height_item, 1, height_edit);
}

void MyTreeWidget::removeAllChildren(QTreeWidgetItem *root)
{
    int item_children_counts = root->childCount();
    while (item_children_counts--)
    {
        QTreeWidgetItem *child = root->child(item_children_counts); //index从大到小区做删除处理
        root->removeChild(child);
        delete child;
        child = nullptr;
    }
}

void MyTreeWidget::refreshResourcesTw()
{
    if (g_pro_data.path == "")
        return;

    removeAllChildren(m_resources_item_root);

    QFileInfoList list = getFileList(g_pro_data.path + QString("/res"));
    int size = list.size();

    m_img_list.clear();
    m_img_list << "";

    for (int i = 0; i < size; ++i)
    {
        QFileInfo fileInfo = list.at(i);        

        QTreeWidgetItem *res_key = new QTreeWidgetItem;
        res_key->setText(0, fileInfo.fileName());
        m_resources_item_root->addChild(res_key);

        if (fileInfo.fileName().right(4) != ".zip")
        {
            m_img_list.append(fileInfo.fileName());
            res_key->setText(0, fileInfo.fileName());
        }
    }
}

void MyTreeWidget::createResourcesTw(Format *format)
{
    Q_UNUSED(format);
    m_resources_item_root = new QTreeWidgetItem;
    m_resources_item_root->setText(0, tr("resources"));
    addTopLevelItem(m_resources_item_root);

    refreshResourcesTw();
}

void MyTreeWidget::createScreensTw(Format *format)
{
    m_screens_item_root = new QTreeWidgetItem;
    m_screens_item_root->setText(0, tr("screens"));
    addTopLevelItem(m_screens_item_root);

    for (auto it = format->screens.begin(); it != format->screens.end(); it++)
    {
        addScreenItemChildren(it.value());
    }
}

void MyTreeWidget::addStreamIndexItemChildren(DataItemChild &parent_item_struct, Stream_index stream_index)
{
    parent_item_struct.all_item = new QTreeWidgetItem;
    parent_item_struct.all_item->setText(0, "all");
    parent_item_struct.stream_index_item_root->addChild(parent_item_struct.all_item);
    QLineEdit *all_edit = new QLineEdit;
    all_edit->setText(QString::number(stream_index.all));
    all_edit->setStyleSheet(m_edit_qss);
    setItemWidget(parent_item_struct.all_item, 1, all_edit);
}
void MyTreeWidget::addMaterialItemChildren(DataItemChild &parent_item_struct, QMap<int, Material> materials)
{
    for (auto it = materials.begin(); it != materials.end(); it++)
    {
        MaterialItemChild one_material_item_child;
        one_material_item_child.material_item_title = new QTreeWidgetItem;
        one_material_item_child.material_item_title->setText(0, "material");

        one_material_item_child.duration_item = new QTreeWidgetItem;
        one_material_item_child.duration_item->setText(0, tr("duration"));
        one_material_item_child.content_item = new QTreeWidgetItem;
        one_material_item_child.content_item->setText(0, tr("content"));

        parent_item_struct.material_item_root->addChild(one_material_item_child.material_item_title);
        parent_item_struct.materials_item_map[it.key()] = one_material_item_child;
        one_material_item_child.material_item_title->addChild(one_material_item_child.duration_item);
        one_material_item_child.material_item_title->addChild(one_material_item_child.content_item);

        QLineEdit *duration_edit = new QLineEdit;
        duration_edit->setText(QString::number(it.value().duration));
        duration_edit->setStyleSheet(m_edit_qss);
        setItemWidget(one_material_item_child.duration_item, 1, duration_edit);

        QLineEdit *content_edit = new QLineEdit;
        content_edit->setText(it.value().content);
        content_edit->setStyleSheet(m_edit_qss);
        QCompleter *completer = new QCompleter(content_list);
        completer->setCaseSensitivity(Qt::CaseInsensitive);
        content_edit->setCompleter(completer);
        setItemWidget(one_material_item_child.content_item, 1, content_edit);

        parent_item_struct.material_item_root->parent()->setExpanded(true);
        parent_item_struct.material_item_root->setExpanded(true);
        one_material_item_child.material_item_title->setExpanded(true);
    }
}

void MyTreeWidget::addCabPosItemItemChildren(ItemItemChild &parent_item_struct, CabPosItem add_cabpositem, QString cabpos)
{
    CabPosItemChild one_cabpositem_item_child;
    one_cabpositem_item_child.cab_pos_item_item_title = new QTreeWidgetItem;
    one_cabpositem_item_child.cab_pos_item_item_title->setText(0, cabpos);

    one_cabpositem_item_child.id_item = new QTreeWidgetItem;
    one_cabpositem_item_child.id_item->setText(0, tr("id"));
    one_cabpositem_item_child.type_item = new QTreeWidgetItem;
    one_cabpositem_item_child.type_item->setText(0, tr("type"));
    one_cabpositem_item_child.x_item = new QTreeWidgetItem;
    one_cabpositem_item_child.x_item->setText(0, tr("x"));
    one_cabpositem_item_child.y_item = new QTreeWidgetItem;
    one_cabpositem_item_child.y_item->setText(0, tr("y"));
    one_cabpositem_item_child.width_item = new QTreeWidgetItem;
    one_cabpositem_item_child.width_item->setText(0, tr("width"));
    one_cabpositem_item_child.height_item = new QTreeWidgetItem;
    one_cabpositem_item_child.height_item->setText(0, tr("height"));
    if (add_cabpositem.type == "text")
    {
        one_cabpositem_item_child.font_item = new QTreeWidgetItem;
        one_cabpositem_item_child.font_item->setText(0, tr("font"));
        one_cabpositem_item_child.size_item = new QTreeWidgetItem;
        one_cabpositem_item_child.size_item->setText(0, tr("size"));
        one_cabpositem_item_child.bold_item = new QTreeWidgetItem;
        one_cabpositem_item_child.bold_item->setText(0, tr("bold"));
        one_cabpositem_item_child.italic_item = new QTreeWidgetItem;
        one_cabpositem_item_child.italic_item->setText(0, tr("italic"));
        one_cabpositem_item_child.fore_color_item = new QTreeWidgetItem;
        one_cabpositem_item_child.fore_color_item->setText(0, tr("force_color"));
        one_cabpositem_item_child.back_color_item = new QTreeWidgetItem;
        one_cabpositem_item_child.back_color_item->setText(0, tr("back_color"));
        one_cabpositem_item_child.background_item = new QTreeWidgetItem;
        one_cabpositem_item_child.background_item->setText(0, tr("background"));
        one_cabpositem_item_child.background_color_item = new QTreeWidgetItem;
        one_cabpositem_item_child.background_color_item->setText(0, tr("background_color"));
        one_cabpositem_item_child.align_item = new QTreeWidgetItem;
        one_cabpositem_item_child.align_item->setText(0, tr("align"));
        one_cabpositem_item_child.effect_item = new QTreeWidgetItem;
        one_cabpositem_item_child.effect_item->setText(0, tr("effect"));
        one_cabpositem_item_child.speed_item = new QTreeWidgetItem;
        one_cabpositem_item_child.speed_item->setText(0, tr("speed"));
    }
    if (add_cabpositem.type == "video")
    {
        one_cabpositem_item_child.sound_item = new QTreeWidgetItem;
        one_cabpositem_item_child.sound_item->setText(0, tr("sound"));
    }

    if (add_cabpositem.type != "video")
    {
        one_cabpositem_item_child.data_item_child.material_item_root = new QTreeWidgetItem;
        one_cabpositem_item_child.data_item_child.material_item_root->setText(0, tr("materials"));
    } else
    {
        one_cabpositem_item_child.data_item_child.stream_index_item_root = new QTreeWidgetItem;
        one_cabpositem_item_child.data_item_child.stream_index_item_root->setText(0, tr("stream_index"));
    }

    one_cabpositem_item_child.data_item = new QTreeWidgetItem;
    one_cabpositem_item_child.data_item->setText(0, tr("data"));

    parent_item_struct.item_item_title->addChild(one_cabpositem_item_child.cab_pos_item_item_title);
    one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.id_item);
    one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.type_item);
    one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.x_item);
    one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.y_item);
    one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.width_item);
    one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.height_item);

    if (add_cabpositem.type == "text")
    {
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.font_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.size_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.bold_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.italic_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.fore_color_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.back_color_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.background_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.background_color_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.align_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.effect_item);
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.speed_item);
    }
    if (add_cabpositem.type == "video")
    {
        one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.sound_item);
    }

    one_cabpositem_item_child.cab_pos_item_item_title->addChild(one_cabpositem_item_child.data_item);

    if (add_cabpositem.type != "video")
        one_cabpositem_item_child.data_item->addChild(one_cabpositem_item_child.data_item_child.material_item_root);
    else
        one_cabpositem_item_child.data_item->addChild(one_cabpositem_item_child.data_item_child.stream_index_item_root);

    QLabel *id_label = new QLabel(QString::number(add_cabpositem.id));
    setItemWidget(one_cabpositem_item_child.id_item, 1, id_label);
    QLabel *type_label = new QLabel(add_cabpositem.type);
    setItemWidget(one_cabpositem_item_child.type_item, 1, type_label);
    QLineEdit *x_edit = new QLineEdit;
    x_edit->setText(QString::number(add_cabpositem.x));
    x_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_cabpositem_item_child.x_item, 1, x_edit);
    QLineEdit *y_edit = new QLineEdit;
    y_edit->setText(QString::number(add_cabpositem.y));
    y_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_cabpositem_item_child.y_item, 1, y_edit);
    QLineEdit *width_edit = new QLineEdit;
    width_edit->setText(QString::number(add_cabpositem.width));
    width_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_cabpositem_item_child.width_item, 1, width_edit);
    QLineEdit *height_edit = new QLineEdit;
    height_edit->setText(QString::number(add_cabpositem.height));
    height_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_cabpositem_item_child.height_item, 1, height_edit);

    if (add_cabpositem.type == "text")
    {
        QLineEdit *font_edit = new QLineEdit;
        font_edit->setText(add_cabpositem.font);
        font_edit->setStyleSheet(m_edit_qss);
        setItemWidget(one_cabpositem_item_child.font_item, 1, font_edit);
        QLineEdit *size_edit = new QLineEdit;
        size_edit->setText(QString::number(add_cabpositem.size));
        size_edit->setStyleSheet(m_edit_qss);
        setItemWidget(one_cabpositem_item_child.size_item, 1, size_edit);
        QComboBox *bold_combo = new MyComboBox;
        bold_combo->insertItems(0, m_combo_text_tf);
        if (add_cabpositem.bold)
            bold_combo->setCurrentIndex(0);
        else
            bold_combo->setCurrentIndex(1);
        setItemWidget(one_cabpositem_item_child.bold_item, 1, bold_combo);

        QComboBox *italic_combo = new MyComboBox;
        italic_combo->insertItems(0, m_combo_text_tf);
        if (add_cabpositem.italic)
            italic_combo->setCurrentIndex(0);
        else
            italic_combo->setCurrentIndex(1);
        setItemWidget(one_cabpositem_item_child.italic_item, 1, italic_combo);
        QLineEdit *fore_color_edit = new QLineEdit;
        fore_color_edit->setText(add_cabpositem.fore_color);
        fore_color_edit->setStyleSheet(m_edit_qss);
        setItemWidget(one_cabpositem_item_child.fore_color_item, 1, fore_color_edit);
        QLineEdit *back_color_edit = new QLineEdit;
        back_color_edit->setText(add_cabpositem.back_color);
        back_color_edit->setStyleSheet(m_edit_qss);
        setItemWidget(one_cabpositem_item_child.back_color_item, 1, back_color_edit);

        QComboBox *background_combo = new MyComboBox;
        background_combo->insertItems(0, m_img_list);
        if (m_img_list.contains(add_cabpositem.background))
            background_combo->setCurrentText(add_cabpositem.background);
        setItemWidget(one_cabpositem_item_child.background_item, 1, background_combo);

        QLineEdit *background_color_edit = new QLineEdit;
        background_color_edit->setText(add_cabpositem.background_color);
        background_color_edit->setStyleSheet(m_edit_qss);
        setItemWidget(one_cabpositem_item_child.background_color_item, 1, background_color_edit);

        QComboBox *align_combo = new MyComboBox;
        align_combo->insertItems(0, m_combo_text_align);
        if (add_cabpositem.align == align_left)
            align_combo->setCurrentIndex(0);
        else if (add_cabpositem.align == align_center)
            align_combo->setCurrentIndex(1);
        else if (add_cabpositem.align == align_right)
            align_combo->setCurrentIndex(2);
        setItemWidget(one_cabpositem_item_child.align_item, 1, align_combo);

        QLineEdit *effect_edit = new QLineEdit;
        effect_edit->setText(QString::number(add_cabpositem.effect));
        effect_edit->setStyleSheet(m_edit_qss);
        setItemWidget(one_cabpositem_item_child.effect_item, 1, effect_edit);
        QLineEdit *speed_edit = new QLineEdit;
        speed_edit->setText(QString::number(add_cabpositem.speed));
        speed_edit->setStyleSheet(m_edit_qss);
        setItemWidget(one_cabpositem_item_child.speed_item, 1, speed_edit);
    }
    if (add_cabpositem.type == "video")
    {
        QComboBox *sound_combo = new MyComboBox;
        sound_combo->insertItems(0, m_combo_text_tf);
        if (add_cabpositem.sound)
            sound_combo->setCurrentIndex(0);
        else
            sound_combo->setCurrentIndex(1);
        setItemWidget(one_cabpositem_item_child.sound_item, 1, sound_combo);
    }

    if (add_cabpositem.type != "video")
        addMaterialItemChildren(one_cabpositem_item_child.data_item_child, add_cabpositem.data.materials);
    else
        addStreamIndexItemChildren(one_cabpositem_item_child.data_item_child, add_cabpositem.data.stream_index);

    parent_item_struct.cab_pos_items_item_map[cabpos] = one_cabpositem_item_child;
}

void MyTreeWidget::addItemItemChildren(LayoutItemChild &parent_item_struct, Item add_item)
{
    ItemItemChild one_item_child;
    one_item_child.item_item_title = new QTreeWidgetItem;
    one_item_child.item_item_title->setText(0, QString("item%1").arg(add_item.id));
    parent_item_struct.item_item_root->addChild(one_item_child.item_item_title);

    for (auto it = add_item.cabpositems.begin(); it != add_item.cabpositems.end(); it++)
        addCabPosItemItemChildren(one_item_child, it.value(), it.key());

    parent_item_struct.items_item_map[add_item.id] = one_item_child;
}

void MyTreeWidget::addLayoutItemChildren(PartitionItemChild &parent_item_struct, Layout add_layout)
{
    LayoutItemChild one_layout_item_child;
    one_layout_item_child.layout_item_title = new QTreeWidgetItem;
    one_layout_item_child.layout_item_title->setText(0, QString("layout%1").arg(add_layout.id));

    one_layout_item_child.id_item = new QTreeWidgetItem;
    one_layout_item_child.id_item->setText(0, tr("id"));
    one_layout_item_child.duration_item = new QTreeWidgetItem;
    one_layout_item_child.duration_item->setText(0, tr("duration"));
    one_layout_item_child.item_item_root = new QTreeWidgetItem;
    one_layout_item_child.item_item_root->setText(0, tr("items"));

    parent_item_struct.layout_item_root->addChild(one_layout_item_child.layout_item_title);
    one_layout_item_child.layout_item_title->addChild(one_layout_item_child.id_item);
    one_layout_item_child.layout_item_title->addChild(one_layout_item_child.duration_item);
    one_layout_item_child.layout_item_title->addChild(one_layout_item_child.item_item_root);

    QLabel *id_label = new QLabel(QString::number(add_layout.id));
    setItemWidget(one_layout_item_child.id_item, 1, id_label);
    QLineEdit *duration_edit = new QLineEdit;
    duration_edit->setText(QString::number(add_layout.duration));
    duration_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_layout_item_child.duration_item, 1, duration_edit);

    for (int i = 0; i < add_layout.items.size(); i++)
        addItemItemChildren(one_layout_item_child, add_layout.items.at(i));
    parent_item_struct.layouts_item_map[add_layout.id] = one_layout_item_child;
}

void MyTreeWidget::addPartitionItemChildren(ScreenItemChild &parent_item_struct, Partition add_partition)
{
    PartitionItemChild one_partition_item_child;
    one_partition_item_child.partition_item_title = new QTreeWidgetItem;
    one_partition_item_child.partition_item_title->setText(0, QString("partition%1").arg(add_partition.id));

    one_partition_item_child.common_item = new QTreeWidgetItem;
    one_partition_item_child.common_item->setText(0, tr("common"));

    one_partition_item_child.id_item = new QTreeWidgetItem;
    one_partition_item_child.id_item->setText(0, tr("id"));
    one_partition_item_child.common_item_child.x_item = new QTreeWidgetItem;
    one_partition_item_child.common_item_child.x_item->setText(0, tr("x"));
    one_partition_item_child.common_item_child.y_item = new QTreeWidgetItem;
    one_partition_item_child.common_item_child.y_item->setText(0, tr("y"));
    one_partition_item_child.common_item_child.width_item = new QTreeWidgetItem;
    one_partition_item_child.common_item_child.width_item->setText(0, tr("width"));
    one_partition_item_child.common_item_child.height_item = new QTreeWidgetItem;
    one_partition_item_child.common_item_child.height_item->setText(0, tr("height"));
    one_partition_item_child.common_item_child.z_order_item = new QTreeWidgetItem;
    one_partition_item_child.common_item_child.z_order_item->setText(0, tr("z_order"));
    one_partition_item_child.common_item_child.transparent_item = new QTreeWidgetItem;
    one_partition_item_child.common_item_child.transparent_item->setText(0, tr("transparent"));
    one_partition_item_child.common_item_child.background_item = new QTreeWidgetItem;
    one_partition_item_child.common_item_child.background_item->setText(0, tr("background"));
    one_partition_item_child.common_item_child.background_color_item = new QTreeWidgetItem;
    one_partition_item_child.common_item_child.background_color_item->setText(0, tr("background_color"));
    one_partition_item_child.layout_item_root = new QTreeWidgetItem;
    one_partition_item_child.layout_item_root->setText(0, tr("layouts"));

    parent_item_struct.partition_item_root->addChild(one_partition_item_child.partition_item_title);
    one_partition_item_child.partition_item_title->addChild(one_partition_item_child.id_item);
    one_partition_item_child.partition_item_title->addChild(one_partition_item_child.common_item);
    one_partition_item_child.common_item->addChild(one_partition_item_child.common_item_child.x_item);
    one_partition_item_child.common_item->addChild(one_partition_item_child.common_item_child.y_item);
    one_partition_item_child.common_item->addChild(one_partition_item_child.common_item_child.width_item);
    one_partition_item_child.common_item->addChild(one_partition_item_child.common_item_child.height_item);
    one_partition_item_child.common_item->addChild(one_partition_item_child.common_item_child.z_order_item);
    one_partition_item_child.common_item->addChild(one_partition_item_child.common_item_child.transparent_item);
    one_partition_item_child.common_item->addChild(one_partition_item_child.common_item_child.background_item);
    one_partition_item_child.common_item->addChild(one_partition_item_child.common_item_child.background_color_item);
    one_partition_item_child.partition_item_title->addChild(one_partition_item_child.layout_item_root);

    QLabel *id_label = new QLabel(QString::number(add_partition.id));
    setItemWidget(one_partition_item_child.id_item, 1, id_label);
    QLineEdit *x_edit = new QLineEdit;
    x_edit->setText(QString::number(add_partition.common.x));
    x_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_partition_item_child.common_item_child.x_item, 1, x_edit);
    QLineEdit *y_edit = new QLineEdit;
    y_edit->setText(QString::number(add_partition.common.y));
    y_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_partition_item_child.common_item_child.y_item, 1, y_edit);
    QLineEdit *width_edit = new QLineEdit;
    width_edit->setText(QString::number(add_partition.common.width));
    width_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_partition_item_child.common_item_child.width_item, 1, width_edit);
    QLineEdit *height_edit = new QLineEdit;
    height_edit->setText(QString::number(add_partition.common.height));
    height_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_partition_item_child.common_item_child.height_item, 1, height_edit);
    QLineEdit *z_order_edit = new QLineEdit;
    z_order_edit->setText(QString::number(add_partition.common.z_order));
    z_order_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_partition_item_child.common_item_child.z_order_item, 1, z_order_edit);
    QComboBox *transparent_combo = new MyComboBox;
    transparent_combo->insertItems(0, m_combo_text_tf);
    if (add_partition.common.transparent)
        transparent_combo->setCurrentIndex(0);
    else
        transparent_combo->setCurrentIndex(1);
    setItemWidget(one_partition_item_child.common_item_child.transparent_item, 1, transparent_combo);

    QComboBox *background_combo = new MyComboBox;
    background_combo->insertItems(0, m_img_list);
    if (m_img_list.contains(add_partition.common.background))
        background_combo->setCurrentText(add_partition.common.background);
    setItemWidget(one_partition_item_child.common_item_child.background_item, 1, background_combo);

    QLineEdit *background_color_edit = new QLineEdit;
    background_color_edit->setText(add_partition.common.background_color);
    background_color_edit->setStyleSheet(m_edit_qss);
    setItemWidget(one_partition_item_child.common_item_child.background_color_item, 1, background_color_edit);

    for (auto it = add_partition.layouts.begin(); it != add_partition.layouts.end(); it++)
    {
        addLayoutItemChildren(one_partition_item_child, it.value());
    }
    parent_item_struct.partitions_item_map[add_partition.id] = one_partition_item_child;
}

void MyTreeWidget::addScreenItemChildren(Screen add_screen)
{
    ScreenItemChild one_screen_item_child;

    one_screen_item_child.screen_item_title = new QTreeWidgetItem;
    one_screen_item_child.screen_item_title->setText(0, QString("screen%1").arg(add_screen.id));

    one_screen_item_child.name_item = new QTreeWidgetItem;
    one_screen_item_child.name_item->setText(0, tr("name"));
    one_screen_item_child.id_item = new QTreeWidgetItem;
    one_screen_item_child.id_item->setText(0, tr("id"));
    one_screen_item_child.partition_item_root = new QTreeWidgetItem;
    one_screen_item_child.partition_item_root->setText(0, tr("partitions"));

    m_screens_item_root->addChild(one_screen_item_child.screen_item_title);
    one_screen_item_child.screen_item_title->addChild(one_screen_item_child.id_item);
    one_screen_item_child.screen_item_title->addChild(one_screen_item_child.name_item);
    one_screen_item_child.screen_item_title->addChild(one_screen_item_child.partition_item_root);

    QLabel *id_label = new QLabel(QString::number(add_screen.id));
    setItemWidget(one_screen_item_child.id_item, 1, id_label);
    QLabel *name_label = new QLabel(add_screen.name);
    setItemWidget(one_screen_item_child.name_item, 1, name_label);

    for (auto it = add_screen.partitions.begin(); it != add_screen.partitions.end(); it++)
    {
        addPartitionItemChildren(one_screen_item_child, it.value());
    }
    //这句话一定要写在addPartitionItemChild后面，否则传值复制导致map内容不会传递到m_screens_item_map中
    m_screens_item_map[add_screen.id] = one_screen_item_child;
}

void MyTreeWidget::setAllItemsLossSelect()
{
    QTreeWidgetItemIterator it(this);
    while (*it)
    {
        (*it)->setSelected(false);
        (*it)->setBackground(0, QBrush("white"));
        (*it)->setBackground(1, QBrush("white"));
        ++it;
    }
}
void MyTreeWidget::setAllItemsReduce(bool flag)
{
    QTreeWidgetItemIterator it(this);
    while (*it)
    {
        (*it)->setExpanded(flag);
        ++it;
    }
}

void MyTreeWidget::autoExpandOrReduceByCurrClickItem(QTreeWidgetItem *curr_item)
{
    while (curr_item != NULL)
    {
        curr_item->setExpanded(true);
        curr_item = curr_item->parent();
    }
}

void MyTreeWidget::setAllChildrenHighLight(QTreeWidgetItem *curr_item)
{
    curr_item->setBackground(0, QColor(225, 255, 255));
    curr_item->setBackground(1, QColor(225, 255, 255));
    for (int i = 0; i < curr_item->childCount(); i++)
    {
        curr_item->child(i)->setBackground(0, QColor(225, 255, 255));
        curr_item->child(i)->setBackground(1, QColor(225, 255, 255));
    }
}

void MyTreeWidget::setSelectedScreenItem(int screen_id)
{
    setAllItemsLossSelect();
    //setAllItemsReduce(false);
    if (m_screens_item_map.contains(screen_id))
    {
        m_screens_item_map[screen_id].screen_item_title->setSelected(true);
        autoExpandOrReduceByCurrClickItem(m_screens_item_map[screen_id].screen_item_title);
        setAllChildrenHighLight(m_screens_item_map[screen_id].screen_item_title);
    }
}

void MyTreeWidget::setSelectedPartitionItem(int screen_id, int partition_id)
{
    setAllItemsLossSelect();
    //setAllItemsReduce(false);
    if (m_screens_item_map.contains(screen_id))
    {
        QTreeWidgetItem *curr_item = m_screens_item_map[screen_id].partitions_item_map[partition_id].partition_item_title;
        curr_item->setSelected(true);
        autoExpandOrReduceByCurrClickItem(curr_item);
        //此处将common展开
        curr_item->child(1)->setExpanded(true);
        setAllChildrenHighLight(curr_item);
    }
}

void MyTreeWidget::setSelectedLayoutItem(int screen_id, int partition_id, int layout_id)
{
    setAllItemsLossSelect();
    //setAllItemsReduce(false);
    if (m_screens_item_map.contains(screen_id))
    {
        QTreeWidgetItem *curr_item = m_screens_item_map[screen_id].partitions_item_map[partition_id].layouts_item_map[layout_id].layout_item_title;
        curr_item->setSelected(true);
        autoExpandOrReduceByCurrClickItem(curr_item);
        setAllChildrenHighLight(curr_item);
    }
}

void MyTreeWidget::setSelectedItemItem(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str)
{
    setAllItemsLossSelect();
    //setAllItemsReduce(false);
    if (m_screens_item_map.contains(screen_id))
    {
        QMap<int, ItemItemChild> map = m_screens_item_map[screen_id].partitions_item_map[partition_id].layouts_item_map[layout_id].items_item_map;
        for (auto it = map.begin(); it != map.end(); it++)
        {
            if (item_id == getIdFromItemText(it.value().item_item_title->text(0)))
            {
                QTreeWidgetItem *curr_item = it.value().cab_pos_items_item_map[cab_pos_item_str].cab_pos_item_item_title;
                curr_item->setSelected(true);
                autoExpandOrReduceByCurrClickItem(curr_item);
                setAllChildrenHighLight(curr_item);
            }
        }
    }
}

void MyTreeWidget::createTreeWidget(Format *format)
{
    if (format->type == "LCD")
        setHeaderLabels(QStringList() << tr("format_lcd") << tr("value")) ;//设置标题
    else
        setHeaderLabels(QStringList() << tr("format_idu") << tr("value")) ;//设置标题

    header()->setSectionResizeMode(QHeaderView::ResizeToContents);

    //path_prefix
    createPathPrefixTw(format);

    //video_streams
    createVideoStreamsTw(format);

    //architecture_version
    createArchitectureVersionTw(format);

    //current_show
    createCurrentShowTw(format);

    //resolution
    createResolutionTw(format);

    //resources
    createResourcesTw(format);

    //screens
    createScreensTw(format);
}
